package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.adapters.BusinessTypeAdapter;
import com.lc.afbx.beans.BusinessType;
import com.lc.afbx.conn.GetApiShowStore;
import com.lc.afbx.util.MapKeyComparator;
import com.lc.afbx.widget.MyListView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilLog;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by Administrator on 2016/5/23.
 */
public class BusinessXubaoPriceActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.imgCompany_syx) private SimpleDraweeView imgCompany_syx;
    @BoundView(R.id.shopName) private TextView shopName;
    @BoundView(R.id.shopAddress) private TextView shopAddress;
    private Button btnXiaYiBu;
    private MyListView listView_business_type_lv;
    private TextView baojia_total_money_tv;
    private TextView baojia_present_tv;
    private TextView baojia_title_tv;
    private ScrollView baojia_sv;

    private List<GetApiShowStore.Data> dataList = new ArrayList<>();
    private List<BusinessType> businessList = new ArrayList<>();
    private BusinessTypeAdapter businessTypeAdapter;
    private Bundle bundle;
    private int selectPosition = -1;

    private Map<Integer, BusinessType> orderMap = new HashMap<>();

    private int totalMoney;

    // 这是保险门店的接口
    private GetApiShowStore getApiShowStore = new GetApiShowStore(new AsyCallBack<GetApiShowStore.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiShowStore.Info info) throws Exception {

            dataList.addAll(info.dataList);

            for (int i = 0; i < dataList.size(); i++) {

                if (dataList.get(i).id.equals(bundle.getString("stores_id"))) {

                    shopName.setText(dataList.get(i).title);
                    shopAddress.setText(dataList.get(i).cur_address);

                    selectPosition = i;

                }

            }

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);
        }
    });

    /**
     * 使用 Map按key进行排序
     * @param map
     * @return
     */
    public static Map<Integer, BusinessType> sortMapByKey(Map<Integer, BusinessType> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        Map<Integer, BusinessType> sortMap = new TreeMap<>(new MapKeyComparator());

        sortMap.putAll(map);

        return sortMap;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shouye_shangyexian_xubao_baojia);

        initView();

        setListener();

        bundle = getIntent().getExtras();

        Map<Integer, BusinessType> typeMap = (Map<Integer, BusinessType>) getIntent().getSerializableExtra("businessType");

        orderMap = sortMapByKey(typeMap);

        for (Map.Entry<Integer, BusinessType> entity : orderMap.entrySet()) {

            Log.e("----", entity.getKey() + "" );

            BusinessType business = entity.getValue();

            totalMoney += Integer.parseInt(business.getMoney());

            businessList.add(business);

        }

        listView_business_type_lv.setFocusable(false);

        listView_business_type_lv.setDivider(null);

        listView_business_type_lv.setAdapter(businessTypeAdapter = new BusinessTypeAdapter(context, businessList));


        getApiShowStore.cid = bundle.getString("cid");
        getApiShowStore.curcode = BaseApplication.BasePreferences.readCityID();
        getApiShowStore.execute(this);

        baojia_total_money_tv.setText(totalMoney + "");
        baojia_title_tv.setText(bundle.getString("company"));
        baojia_present_tv.setText(" 优惠信息：" + bundle.getString("present"));

        Log.e("imgUrl", bundle.getString("imgUrl"));
//        imgCompany_syx.setImageURI(Uri.parse(bundle.getString("imgUrl")));

        baojia_sv.smoothScrollTo(0, 0);

    }

    private void setListener() {

        btnXiaYiBu.setOnClickListener(this);

    }

    private void initView() {

        findViewById(R.id.back).setOnClickListener(this);
        btnXiaYiBu = ((Button) findViewById(R.id.shouye_btn_shangyexian_baojia_lijitoubao));

        listView_business_type_lv = ((MyListView) findViewById(R.id.listView_business_type_lv));
        baojia_total_money_tv = ((TextView) findViewById(R.id.baojia_total_money_tv));
        baojia_title_tv = ((TextView) findViewById(R.id.baojia_title_tv));
        baojia_present_tv = ((TextView) findViewById(R.id.baojia_present_tv));
        baojia_sv = ((ScrollView) findViewById(R.id.baojia_sv));
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.back:
                finish();
                return;

            case R.id.shouye_btn_shangyexian_baojia_lijitoubao:

                if (selectPosition < 0) {

                    UtilToast.show(this, "请选择一个门店");
                    return;

                }
                UtilLog.e("===========================2", bundle.getString("insurance_type") + "sdfasdfsaasdfas");
                bundle.putString("shopName", dataList.get(selectPosition).title);
                bundle.putString("shopId", dataList.get(selectPosition).id);
                bundle.putString("totalMoney", totalMoney + "");

                startActivity(new Intent(this, BusinessInsuranceXubaoInsureActivity.class).putExtras(bundle));

                break;
        }


    }


}
